package com.authentication.manager;

import com.authentication.LoginSuccessAuthentication;
import com.authentication.RBACUser;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Arrays;
import java.util.Collections;

import static com.common.Constant.ROLE_PREFIX;

public class InMemoryAuthenticationManager implements AuthenticationManager {


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {


        String userName = authentication.getPrincipal().toString();
        RBACUser rbacUser = new RBACUser(
                userName,
                10L,
                996L
        );

        if ("yan".equals(userName)) {
            return new LoginSuccessAuthentication(
                    null,
                    rbacUser,
                    Arrays.asList(
                            new SimpleGrantedAuthority(ROLE_PREFIX+"stuff"),
                            new SimpleGrantedAuthority("update_order")
                    )
            );
        }

        return new LoginSuccessAuthentication(
                null,
                rbacUser,
                Arrays.asList(
                        new SimpleGrantedAuthority("admin"),
                        new SimpleGrantedAuthority("stuff"),
                        new SimpleGrantedAuthority("user"),
                        new SimpleGrantedAuthority("guest")
                )
        );
    }
}
